﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using DFYE.Web.Models;
using JULONG.WEB.Library;

namespace DFYE.Web.Areas.manage.Controllers
{

    public class SiteLibraryController : Controller
    {
        //
        // GET: /manage/intro/
        DataSession<entityDBContainer,SiteLibrary> db = new DataSession<entityDBContainer, SiteLibrary>();

        [HttpGet]
        public ActionResult Edit(string id)
        {
            string name = "";
            string[] strs = id.Split(',');
            
            string symbol = strs[0];
            if (strs.Length > 1) { 
                name = strs[1];
            }

            if (string.IsNullOrWhiteSpace(symbol)) {
                return View();
            }
            SiteLibrary symbolDoc = db.query(d => d.symbol == symbol).FirstOrDefault();

            if (symbolDoc == null) {
                symbolDoc = db.create();
                symbolDoc.symbol=symbol;
                symbolDoc.name = name;
                db.insert(symbolDoc);
            }
            id = symbolDoc.ID.ToString();
            return View(symbolDoc);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(int id,FormCollection fc)
        {
            SiteLibrary symbolDoc = db.query(d => d.ID == id).First();
            if (symbolDoc != null)
            {
                TryUpdateModel(symbolDoc);
                db.save();
            }

            return View(symbolDoc);
        }
    }
}
